package fm.dongman.modellib.source;


import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.contractlib.contract.ArticleInfoContract;
import fm.dongman.modellib.RemoteHelper;
import fm.dongman.modellib.StaticArea.StaticUrl;
import fm.dongman.modellib.remote.RemoteOperationHelper;

/**
 * @author alex on 2017/10/28.
 */

public class ArticleInfoRepository implements ArticleInfoContract.IArticleInfoDataSource{
    private static ArticleInfoRepository INSTANCE;
    private String mArticleId;  //文章Id
    private boolean isRemote = true;

    private ArticleInfoRepository(){}

    public static ArticleInfoRepository getInstance(){
        if (INSTANCE==null){
            INSTANCE=new ArticleInfoRepository();
        }
        return INSTANCE;
    }

    @Override
    public void setArticleId(@NotNull String id) {
        mArticleId=id;
    }

    @Override
    public void getData(@NotNull LoadSourceCallback<? super IModel.IArticleModel> callback) {
        if (isRemote) {
            JSONObject object = RemoteHelper.Companion.getInstance().parseGetUrl(StaticUrl.ArticleInfo + "id=" + mArticleId);
            if (object == null) {
                callback.onDataLoadFiled();
                return;
            }
            callback.onDataLoaded(RemoteOperationHelper.getOperationHelper().downArticleDetails(object));
        }else callback.onDataLoadFiled();
    }

    @Override
    public void saveData(IModel.IArticleModel dataModel) {
    }

    @Override
    public void refreshData() {
    }

    @Override
    public void offLine() {
        isRemote = !isRemote;
    }
}
